﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dm.Domain.Repository.RepositoryBase;
using Dm.Domain.Entities;
using Dm.Domain.Entities.EntityBases;
using System.Data.Entity;

namespace Dm.Domain.Repository.SearchInterface
{
    public class SearchManager<TEntity> where TEntity : PersonBase
    {
        private BaseContext _context;
        private IDbSet<TEntity> _dbSet;
        public SearchManager(BaseContext context)
        {
            _context = context;
            _dbSet = _context.Set<TEntity>();
        }

        public IQueryable<TEntity> SearchByName(string firstName, string middleName, string lastName)
        {
            return _dbSet.Where(c =>
                (c.FirstName.Contains(firstName)) &&
                (c.MiddleName.Contains(middleName)) &&
                (c.LastName.Contains(lastName)));
        }

        public IQueryable<TEntity> SearchAllParameter(string firstName, string middleName, string lastName, string studentNumber, string email, string contactNumber, string address)
        {
            return _dbSet.Where(s =>
                ((string.IsNullOrEmpty(firstName)) ? true : s.FirstName.Contains(firstName)) &&
                ((string.IsNullOrEmpty(middleName)) ? true : s.MiddleName.Contains(middleName)) &&
                ((string.IsNullOrEmpty(lastName)) ? true : s.LastName.Contains(lastName)) &&
                ((string.IsNullOrEmpty(studentNumber)) ? true : s.ControlNumber.Contains(studentNumber)) &&
                ((string.IsNullOrEmpty(email)) ? true : s.Email.Contains(email)) &&
                ((string.IsNullOrEmpty(contactNumber)) ? true : s.ContactNumber.Contains(contactNumber)) &&
                ((string.IsNullOrEmpty(address)) ? true : s.Address.AddressLine1.Contains(address)));
        }
    }
}